﻿/*
 * ========================================================================
 * Copyright(c) 2008-2010北京高阳金信信息技术有限公司, All Rights Reserved.
 * ========================================================================
 *  Silverlight工具类
 * 
 * 作者：张生     时间：2010年6月9日11:21:57
 * 版本：V1.0
 * 
 * 修改者：         时间：                
 * 修改说明：
 * ========================================================================
*/
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Common
{
    public class Utils
    {
        /// <summary>
        /// 获取对象大小值
        /// </summary>
        /// <param name="o">源对象</param>
        /// <returns>返回对象大小</returns>
        public static Size GetSize(object o)
        {
            FrameworkElement e = (FrameworkElement)o;
            double w = e.ActualWidth;
            double h = e.ActualHeight; 
            if (o.ToString().IndexOf(".Line") > -1)
            {
                Line l = (Line)o;
                w = Math.Abs(l.X1 - l.X2);
                h = Math.Abs(l.Y1 - l.Y2);
            }
            else
            {
                if (w.Equals(0) || w.Equals(double.NaN)) w = e.Width;
                if (h.Equals(0) || h.Equals(double.NaN)) h = e.Height;
                if (w.Equals(0) || w.Equals(double.NaN)) w = (double)e.GetValue(Canvas.ActualWidthProperty);
                if (h.Equals(0) || h.Equals(double.NaN)) h = (double)e.GetValue(Canvas.ActualHeightProperty);
                if (w.Equals(0) || w.Equals(double.NaN)) w = (double)e.GetValue(Canvas.WidthProperty);
                if (h.Equals(0) || h.Equals(double.NaN)) h = (double)e.GetValue(Canvas.HeightProperty);
                if (w.Equals(double.NaN)) w = 0;
                if (h.Equals(double.NaN)) h = 0;
            }
            return new Size(w, h);
        }
        /// <summary>
        /// 设置对象大小值
        /// </summary>
        /// <param name="o">源对象</param>
        /// <param name="s">对象宽高</param>
        public static void SetSize(object o, Size s)
        {
            FrameworkElement e = (FrameworkElement)o;
            e.Width = s.Width;
            e.Height = s.Height;
        }

        /// <summary>
        /// 获取对象坐标值
        /// </summary>
        /// <param name="o">源对象</param>
        /// <returns>返回对象坐标</returns>
        public static Point GetPoint(object o)
        {
            FrameworkElement e = (FrameworkElement)o;
            double x = Canvas.GetLeft(e);
            double y = Canvas.GetTop(e);
            if (o.ToString().IndexOf(".Line") > -1)
            {
                Line l = (Line)o;
                x = l.X1;
                y = l.Y1;
            }
            return new Point(x, y);
        }
        /// <summary>
        /// 设置对象坐标值
        /// </summary>
        /// <param name="o">源对象</param>
        /// <param name="p">对象坐标</param>
        public static void SetPoint(object o, Point p)
        {
            FrameworkElement e = (FrameworkElement)o;
            Canvas.SetLeft(e, p.X);
            Canvas.SetTop(e, p.Y);
        }

        /// <summary>
        /// 获取当前坐标是否在所在范围内
        /// </summary>
        /// <param name="p">当前坐标</param>
        /// <param name="r">范围</param>
        /// <returns>返回当前坐标是否在所在范围内</returns>
        public static bool GetArea(Point p, Rect r)
        {
            return (p.X > r.Left && p.X < r.Right && p.Y > r.Top && p.Y < r.Bottom);
        }

        /// <summary>
        /// 获取对象ZIndex值
        /// </summary>
        /// <param name="o">源对象</param>
        /// <returns>返回对象ZIndex值</returns>
        public static int GetZIndex(object o)
        {
            FrameworkElement e = (FrameworkElement)o;
            int z = Canvas.GetZIndex(e);
            return z;
        }
        /// <summary>
        /// 设置对象ZIndex值
        /// </summary>
        /// <param name="o">源对象</param>
        /// <param name="z">对象ZIndex值</param>
        public static void SetZIndex(object o, int z)
        {
            FrameworkElement e = (FrameworkElement)o;
            Canvas.SetZIndex(e, z);
        }

        /// <summary>
        /// 获取对象坐标值及ZIndex值
        /// </summary>
        /// <param name="o">源对象</param>
        /// <returns>返回坐标值及ZIndex值</returns>
        public static Property GetProperty(object o)
        {
            Point p = Utils.GetPoint(o);
            int z = Utils.GetZIndex(o);
            return new Property(p.X, p.Y, z);
        }
        /// <summary>
        /// 设置对象坐标值及ZIndex值
        /// </summary>
        /// <param name="o">源对象</param>
        /// <param name="p">对象坐标及ZIndex值</param>
        public static void SetProperty(object o, Property p)
        {
            Utils.SetPoint(o, new Point(p.X, p.Y));
            Utils.SetZIndex(o, p.Z);
        }

        /// <summary>
        /// URL参数设置
        /// </summary>
        /// <param name="p">现有参数</param>
        /// <param name="k">参数名称</param>
        /// <param name="v">参数值</param>
        /// <returns>返回URL</returns>
        public static string GetParam(string p, string k, string v)
        {
            return GetParam(p, k, v, null);
        }

        /// <summary>
        /// URL参数设置
        /// </summary>
        /// <param name="p">现有参数</param>
        /// <param name="k">参数名称</param>
        /// <param name="v">参数值</param>
        /// <param name="d">泛型集合</param>
        /// <returns>返回URL</returns>
        public static string GetParam(string p, string k, string v, Dictionary<string, string> d)
        {
            if (!v.Equals(""))
            {
                if (p.Length > 0) p += "&";
                p += k + "=" + v;
                if (d != null)
                {
                    if (d.ContainsKey(k))
                        d[k] = v;
                    else
                        d.Add(k, v);
                }
            }
            return p;
        }

        /// <summary>
        /// (给元素)设置Grid单元格(行)
        /// </summary>
        /// <param name="e">元素</param>
        /// <param name="r">行号</param>
        private static void SetGridRow(FrameworkElement e, int r)
        {
            Grid.SetRow(e, r);
        }
        /// <summary>
        /// 设置Grid单元格(行及合并行)
        /// </summary>
        /// <param name="e">元素</param>
        /// <param name="r">行号</param>
        /// <param name="s">合并行数</param>
        private static void SetGridRowSpan(FrameworkElement e, int r, int s)
        {
            Utils.SetGridRow(e, r);
            Grid.SetRowSpan(e, s);
        }
        /// <summary>
        /// 设置Grid单元格(行及合并行与列)
        /// </summary>
        /// <param name="e">元素</param>
        /// <param name="r">行号</param>
        /// <param name="rs">合并行数</param>
        /// <param name="c">列号</param>
        public static void SetGridRowSpan(FrameworkElement e, int r, int rs, int c)
        {
            Utils.SetGridRowSpan(e, r, rs);
            Utils.SetGridColumn(e, c);
        }
        /// <summary>
        /// 设置Grid单元格(行及合并行与列，加入到Grid对象中)
        /// </summary>
        /// <param name="g">Grid</param>
        /// <param name="e">元素</param>
        /// <param name="r">行号</param>
        /// <param name="rs">合并行数</param>
        /// <param name="c">列号</param>
        public static void SetGridRowSpan(Grid g, FrameworkElement e, int r, int rs, int c)
        {
            Utils.SetGridRowSpan(e, r, rs, c);
            g.Children.Add(e);
        }
        /// <summary>
        /// 设置Grid单元格(列)
        /// </summary>
        /// <param name="e">元素</param>
        /// <param name="c">列号</param>
        private static void SetGridColumn(FrameworkElement e, int c)
        {
            Grid.SetColumn(e, c);
        }
        /// <summary>
        /// 设置Grid单元格(列及合并列)
        /// </summary>
        /// <param name="e">元素</param>
        /// <param name="c">列号</param>
        /// <param name="s">合并列数</param>
        private static void SetGridColumnSpan(FrameworkElement e, int c, int s)
        {
            Utils.SetGridColumn(e, c);
            Grid.SetColumnSpan(e, s);
        }
        /// <summary>
        /// 设置Grid单元格(列及合并列与行)
        /// </summary>
        /// <param name="e">元素</param>
        /// <param name="r">行号</param>
        /// <param name="c">列号</param>
        /// <param name="cs">合并列数</param>
        public static void SetGridColumnSpan(FrameworkElement e, int r, int c, int cs)
        {
            Utils.SetGridRow(e, r);
            Utils.SetGridColumnSpan(e, c, cs);
        }
        /// <summary>
        /// 设置Grid单元格(列及合并列与行，加入到Grid对象中)
        /// </summary>
        /// <param name="g">Grid</param>
        /// <param name="e">元素</param>
        /// <param name="r">行号</param>
        /// <param name="c">列号</param>
        /// <param name="cs">合并列数</param>
        public static void SetGridColumnSpan(Grid g, FrameworkElement e, int r, int c, int cs)
        {
            Utils.SetGridColumnSpan(e, r, c, cs);
            g.Children.Add(e);
        }
        /// <summary>
        /// 设置Grid单元格(列与行)
        /// </summary>
        /// <param name="e">元素</param>
        /// <param name="r">行号</param>
        /// <param name="c">列号</param>
        public static void SetGrid(FrameworkElement e, int r, int c)
        {
            Utils.SetGridRow(e, r);
            Utils.SetGridColumn(e, c);
        }
        /// <summary>
        /// 设置Grid单元格(列与行，加入到Grid对象中)
        /// </summary>
        /// <param name="g">Grid</param>
        /// <param name="e">元素</param>
        /// <param name="r">行号</param>
        /// <param name="c">列号</param>
        public static void SetGrid(Grid g, FrameworkElement e, int r, int c)
        {
            Utils.SetGrid(e, r, c);
            g.Children.Add(e);
        }
        /// <summary>
        /// 设置Grid单元格(列及合并列与行及合并行)
        /// </summary>
        /// <param name="e">元素</param>
        /// <param name="r">行号</param>
        /// <param name="rs">合并行数</param>
        /// <param name="c">列号</param>
        /// <param name="cs">合并列数</param>
        private static void SetGrid(FrameworkElement e, int r, int rs, int c, int cs)
        {
            Utils.SetGridRowSpan(e, r, rs);
            Utils.SetGridColumnSpan(e, c, cs);
        }

        /// <summary>
        /// 控件Transform操作
        /// </summary>
        /// <param name="ts">Transform数据，List类型传入</param>
        /// <returns>TransformGroup</returns>
        public static TransformGroup Transform(List<Transform> ts)
        {
            TransformGroup tg = new TransformGroup();
            foreach (Transform t in ts)
                tg.Children.Add(t);
            return tg;
        }

        /// <summary>
        /// 控件Transform操作
        /// </summary>
        /// <param name="target">当前元素</param>
        /// <param name="transformName">待查找的TransformGroup类型，包括：TranslateTransform、ScaleTransform、SkewTransform、RotateTransform</param>
        /// <returns></returns>
        public static Transform FindTransform(FrameworkElement target, string transformName)
        {
            foreach (Transform t in ((TransformGroup)target.RenderTransform).Children)
            {
                if (t.GetType().Name.Equals(transformName)) return t;
            }
            return new TransformGroup();
        }

        /// <summary>
        /// 控件Transform操作
        /// </summary>
        /// <param name="target">当前元素</param>
        public static TransformGroup FindTransformGroup(FrameworkElement target)
        {
            if (target.RenderTransform as TransformGroup != null)
            {
                return (TransformGroup)target.RenderTransform;
            }
            return new TransformGroup();
        }

        /// <summary>
        /// 控件Transform操作
        /// </summary>
        /// <param name="target">当前元素</param>
        public static TranslateTransform ElementTranslate(FrameworkElement target)
        {
            TranslateTransform tt = null;
            try { tt = (TranslateTransform)FindTransform(target, "TranslateTransform"); }
            catch
            {
                tt = new TranslateTransform();
                TransformGroup tg = FindTransformGroup(target);
                tg.Children.Add(tt);
                target.RenderTransform = tg;
            }
            return tt;
        }

        /// <summary>
        /// 控件Transform操作
        /// </summary>
        /// <param name="target">当前元素</param>
        public static ScaleTransform ElementScale(FrameworkElement target)
        {
            ScaleTransform st = null;
            try { st = (ScaleTransform)FindTransform(target, "ScaleTransform"); }
            catch
            {
                st = new ScaleTransform();
                TransformGroup tg = FindTransformGroup(target);
                tg.Children.Add(st);
                target.RenderTransform = tg;
            }
            return st;
        }

        /// <summary>
        /// 控件Transform操作
        /// </summary>
        /// <param name="target">当前元素</param>
        public static SkewTransform ElementSkew(FrameworkElement target)
        {
            SkewTransform st = null;
            try { st = (SkewTransform)FindTransform(target, "SkewTransform"); }
            catch
            {
                st = new SkewTransform();
                TransformGroup tg = FindTransformGroup(target);
                tg.Children.Add(st);
                target.RenderTransform = tg;
            }
            return st;
        }

        /// <summary>
        /// 控件Transform操作
        /// </summary>
        /// <param name="target">当前元素</param>
        public static RotateTransform ElementRotate(FrameworkElement target)
        {
            RotateTransform rt = null;
            try { rt = (RotateTransform)FindTransform(target, "RotateTransform"); }
            catch
            {
                rt = new RotateTransform();
                TransformGroup tg = FindTransformGroup(target);
                tg.Children.Add(rt);
                target.RenderTransform = tg;
            }
            return rt;
        }

        /// <summary>
        /// 身份证号取生日
        /// </summary>
        /// <param name="o">身份证号(15、18位)</param>
        /// <returns></returns>
        public static DateTime GetCardToDate(object o)
        {
            DateTime d = DateTime.Now;
            try
            {
                string s = o.ToString().Replace("\"", ""), r = "";
                if (s.Length == 15)
                    r = (int.Parse(s.Substring(6, 2)) + 1900) + s.Substring(8, 4);
                else if (s.Length == 18)
                    r = s.Substring(6, 8);
                if (!r.Equals("")) d = DateTime.Parse(r.Substring(0, 4) + "-" + r.Substring(4, 2) + "-" + r.Substring(6, 2));
            }
            catch { }
            return d;
        }

        /// <summary>
        /// 获取时间
        /// </summary>
        /// <param name="y">年</param>
        /// <param name="m">月</param>
        /// <param name="d">日</param>
        /// <returns>返回格式化日期</returns>
        private static DateTime GetDateTime(int y, int m, int d)
        {
            try { DateTime datetime = DateTime.Parse(y + "-" + m + "-" + d); }
            catch { y = DateTime.Now.Year; m = DateTime.Now.Month; d = DateTime.Now.Day; }
            return DateTime.Parse(y + "-" + m + "-" + d);
        }

        /// <summary>
        /// 获取某年的第N周的第一天
        /// </summary>
        /// <param name="y">年</param>
        /// <param name="n">周</param>
        /// <returns>某年的第N周的第一天</returns>
        public static DateTime GetWeekFirstDay(int y, int n)
        {
            DateTime d = Utils.GetDateTime(y, 1, 1);
            return d.AddDays(((n - 1) * 7 - Convert.ToInt32(d.DayOfWeek)));
        }

        /// <summary>
        /// 获取某年的第N月的第一天
        /// </summary>
        /// <param name="y">年</param>
        /// <param name="m">月</param>
        /// <returns>某年的第N月的第一天</returns>
        public static DateTime GetMonthFirstDay(int y, int m)
        {
            DateTime d = Utils.GetDateTime(y, m, 1);
            return d;
        }

        /// <summary>
        /// 计算某年周数
        /// </summary>
        /// <param name="y">年</param>
        /// <returns>某年周数</returns>
        public static int GetWeek(int y)
        {
            DateTime d = Utils.GetDateTime(y, 1, 1);
            y = d.AddYears(1).AddDays(-1).DayOfYear;
            return (Convert.ToInt32(d.DayOfWeek) == 0 ? (y / 7 + 1) : (y / 7 + 2));
        }

        /// <summary>
        /// 计算某时间为第几周
        /// </summary>
        /// <param name="y">年</param>
        /// <param name="m">月</param>
        /// <param name="d">日</param>
        /// <returns>某时间为第几周</returns>
        public static int GetWeek(int y, int m, int d)
        {
            DateTime d1 = Utils.GetDateTime(y, m, d);
            DateTime d2 = Utils.GetDateTime(y, 1, 1);
            int i = Convert.ToInt32(d1.DayOfWeek);
            int j = Convert.ToInt32(d2.DayOfWeek);
            int w = (d1.DayOfYear % 7 == 0 ? (d1.DayOfYear / 7) : (d1.DayOfYear / 7 + 1));
            if (j > 0 && j > i) w++;
            return w;
        }

        /// <summary>
        /// 获取某日期为星期几
        /// </summary>
        /// <param name="y">年</param>
        /// <param name="m">月</param>
        /// <param name="d">日</param>
        /// <returns>某日期为星期几</returns>
        public static int GetWeeks(int y, int m, int d)
        {
            int w = 0;
            switch (Utils.GetDateTime(y, m, d).DayOfWeek)
            {
                case DayOfWeek.Sunday: w = 7; break;
                case DayOfWeek.Monday: w = 1; break;
                case DayOfWeek.Tuesday: w = 2; break;
                case DayOfWeek.Wednesday: w = 3; break;
                case DayOfWeek.Thursday: w = 4; break;
                case DayOfWeek.Friday: w = 5; break;
                case DayOfWeek.Saturday: w = 6; break;
            }
            return w;
        }

        /// <summary>
        /// 某月多少天
        /// </summary>
        /// <param name="y">年</param>
        /// <param name="m">月</param>
        /// <returns>某月多少天</returns>
        public static int GetMonthDays(int y, int m)
        {
            DateTime d = Utils.GetDateTime(y, m, 1);
            return DateTime.DaysInMonth(d.Year, d.Month);
        }

        /// <summary>
        /// 取积值
        /// </summary>
        /// <param name="s">因数</param>
        /// <param name="g">因数</param>
        /// <returns>取积值</returns>
        public static double GetMultiply(double s, double g)
        {
            return s * g;
        }

        /// <summary>
        /// 取模值
        /// </summary>
        /// <param name="i">源值</param>
        /// <param name="m">模值</param>
        /// <returns>取模值</returns>
        public static int GetModulus(int i, int m)
        {
            return (i % m == 0 ? m : i % m);
        }

        /// <summary>
        /// 判断控件是否已生成数据
        /// </summary>
        /// <param name="d">现有值</param>
        /// <param name="c">比较值</param>
        /// <returns>控件是否已生成数据</returns>
        public static bool GetCreate(object d, object c)
        {
            bool flag = false;
            if (d != null)
            {
                flag = (d.ToString().IndexOf(c.ToString()) > -1 ? true : false);
            }
            return flag;
        }

        /// <summary>
        /// 检测是否是正确的Url
        /// </summary>
        /// <param name="strUrl">要验证的Url</param>
        /// <returns>是否是正确的Url</returns>
        public static bool IsURL(string strUrl)
        {
            return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
        }

        /// <summary>
        /// 背景网格线
        /// </summary>
        /// <param name="c">父控件</param>
        /// <param name="s">父控件大小</param>
        /// <param name="f">是否显示网格线</param>
        public static void BackGrid(Canvas c, Size s, bool b, bool l)
        {
            c.Children.Clear();

            if (b)
                c.Children.Add(new Border() { Width = s.Width, Height = s.Height, BorderThickness = new Thickness(1), BorderBrush = new SolidColorBrush(Colors.LightGray) });

            if (l)
            {
                double top = 0;
                double left = 0;
                SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(255, 225, 225, 225));
                double thickness = 0.3;
                double stepLength = 10;

                double x = left + stepLength;
                double y = top;
                while (x < s.Width + left)
                {
                    Line line = new Line() { X1 = x, Y1 = y, X2 = x, Y2 = y + s.Height, Stroke = brush, StrokeThickness = thickness, Stretch = Stretch.Fill };
                    c.Children.Add(line);
                    x += stepLength;
                }
                x = left;
                y = top + stepLength;
                while (y < s.Height + top)
                {
                    Line line = new Line() { X1 = x, Y1 = y, X2 = x + s.Width, Y2 = y, Stroke = brush, StrokeThickness = thickness, Stretch = Stretch.Fill };
                    c.Children.Add(line);
                    y += stepLength;
                }
            }
        }
        
        /// <summary>
        /// 获取当前页面大小
        /// </summary>
        /// <returns>返回当前页面大小</returns>
        public static Size GetCurrentCanvasSize()
        {
            double width = Application.Current.Host.Content.ActualWidth;
            double hight = Application.Current.Host.Content.ActualHeight;
            return new Size(width, hight);
        }
    }


    /// <summary>
    /// 对象坐标及ZIndex
    /// </summary>
    public class Property
    {
        private double x = 0;
        private double y = 0;
        private int z = 0;

        public double X
        {
            get { return x; }
            set { x = value; }
        }
        public double Y
        {
            get { return y; }
            set { y = value; }
        }
        public int Z
        {
            get { return z; }
            set { z = value; }
        }

        public Property() { }

        public Property(double x, double y, int z) { this.x = x; this.y = y; this.z = z; }
    }
}
